package com.leetcode.LC_MED;

import com.leetcode.LC.LC200;

public class LC547 {
    public int findCircleNum(int[][] isConnected) {

        int count = 0;
        for (int i = 0; i < isConnected.length; i++) {
            for (int j = 0; j < isConnected[0].length; j++) {
                if (isConnected[i][j] == 1) {
                    count++;
                    dfs(isConnected, i, j);
                }
            }
        }
        return count;
    }

    private void dfs(int[][] isConnected, int x, int y) {
        if (x < 0 || x >= isConnected.length || y < 0 || y >= isConnected[0].length || isConnected[x][y] == 0) {
            return;
        }
        isConnected[x][y] = 0;
        dfs(isConnected, x - 1, y);
        dfs(isConnected, x + 1, y);
        dfs(isConnected, x, y - 1);
        dfs(isConnected, x, y + 1);
    }

    public static void main(String[] args) {
        System.out.println(new LC200().numIslands(new char[][]{
                {'1', '1', '0'},
                {'1', '1', '0'},
                {'0', '0', '1'}
        }));
    }
}
